// pages/confirmBilling/confirmBilling.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    index: 0, //下拉列表得index
    // 上个界面选择的数据
    data: {},
    typeData: [{
        name: '个人',
      },
      {
        name: '企业',
      }
    ],
    //选择了那个发票类型
    typeName: '',
    // 类型切换
    currentIndex: 0,
    // 邮箱
    isemail: '',
    // 种类切换
    typeIndex: 0,
    // 种类选择
    billingType: [{
        billingName: '增值税专用发票'
      },
      {
        billingName: '增值税电子发票'
      }
    ],
    // 种类选择了那个名字
    billingName: '',
    bid:'',
    fee:'',
   onnum:[],
   names:[],
   EMail:[],
   Bank:[],
   BankCard:[],
   Address:[],
   Tel:[],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    console.log(111111);
    let that = this;
    that.setData({
      bid:options.BID,
      fee:options.Fee
    })
    let   token= wx.getStorageSync('token')
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
      data: {
        cmd: 'FindFpTitle',
        token: token,
       
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res)
          let   onnum = []
          let   names = []
          let EMail = []
          let Bank = []
          let BankCard = []
          let Address = []
          let Tel = []
          res.data.DATA.forEach((item, index) => {
            names[index] = item.FpTitle
            onnum[index] = item.TaxNo
            EMail[index] = item.EMail
            Bank[index] = item.Bank
            BankCard[index] = item.BankCard
            Address[index] = item.Address
            Tel[index] = item.Tel

          })
        that.setData({
          onnum:onnum,
          names:names,
          EMail,
          Bank,
          BankCard,
          Address,
          Tel
        })
      }
    })

    
    
  },

  onnavigateon(e){
    wx.navigateTo({
      url: '/pages/invoiceTitle/invoiceTitle',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 回退
  navigateBack() {
    wx.navigateBack();
  },
  listenerPickerSelected(e) {
    console.log(e);
    //改变index值，通过setData()方法重绘界面
    this.setData({
      index: e.detail.value
    });
  },
  // 类型选择
  selectType(e) {
    // 切换样式,选择了个人还是企业
    this.setData({
      currentIndex: e.currentTarget.dataset.index,
      typeName: e.currentTarget.dataset.name
    })
  },
  // 发票种类
  selectBillingName(e) {
    // 切换样式,选择了普通还是企业
    this.setData({
      typeIndex: e.currentTarget.dataset.index,
      billingName: e.currentTarget.dataset.name
    })
  },
  // 发送邮件
  onemail(e) {
    console.log(e);
    this.setData({
      isemail: e.detail.value
    })
  },
  submit() {
    let that = this;
    let isemail = that.data.isemail;

    let Iscorp = that.data.currentIndex;
    let bid = that.data.bid;
    let FpTitle = that.data.names;
    let taxno = that.data.onnum;
    let index = that.data.index;
    let tfptype = that.data.typeIndex;
    let EMail = that.data.EMail;
    let Bank = that.data.Bank;
    let BankCard = that.data.BankCard;
    let Address = that.data.Address;
    let Tel = that.data.Tel;

    let token= wx.getStorageSync('token')
    
        // 如果是个人
        if (that.data.currentIndex === 0) {
          if (!isemail) {
          wx.showToast({
            title: '邮箱不能为空',
            icon: 'error'
          })
          return;
        }else if(!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(isemail))){
          wx.showToast({
          title: '邮箱输入有误',
          duration: 2000,
          icon: 'none'
          });
          return
          }
          else{
          wx.request({
            url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
            data: {
              cmd: 'SubmitBill',
              token: token,
              IsCorp:Iscorp,
              Email:isemail,
              BID:bid,
             
            },
            method: "get",
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              console.log(res);
              wx.showToast({
              title: res.data.DATA.msg,
              duration: 2000,
              icon: 'success'
              });
             setTimeout(function(){
               let coinfo = false;
               let coinfoout = true;
              wx.navigateTo({
                url: '/pages/controlInvoices/controlInvoices?kpinfo=' + 'false' + '&kpinfobott=' + 'true' + '&openlist=' + 1,
              })
             },2000)
              return              
            }
          })
        }
        } else if (that.data.currentIndex === 1) {
          // 如果是企业
          if(FpTitle.length == 0){
            wx.showToast({
              title: '请到发票抬头管理添加发票信息',
              duration: 2000,
              icon: 'none'
              });
              return
          }else{
            wx.request({
              url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
              data: {
                cmd: 'SubmitBill',
                token: token,
                IsCorp:Iscorp,
                FpType:tfptype,
                FpTitle:FpTitle[index],
                TaxNo:taxno[index],
                Email:EMail[index],
                BID:bid,
                Bank:Bank[index],
                BankCard:BankCard[index],
                Address:Address[index],
                Tel:Tel[index],
               
              },
              method: "get",
              header: {
                'content-type': 'application/json' // 默认值
              },
              success (res) {
                console.log(res);
                wx.showToast({
                title: res.data.DATA.msg,
                duration: 2000,
                icon: 'success'
                });
                setTimeout(function(){
                  wx.navigateTo({
                    url: '/pages/controlInvoices/controlInvoices?kpinfo=' + false + '&kpinfobott=' +true + '&openlist=' + 1,
                  })
                 },2000)
                return
  
              }
            })
          }
        }

        // wx.showToast({
        //   title: '成功开票',
        //   duration: 2000,
        //   icon: 'success'
        //   });
        //   return
      // }
    
  }
})